<template>
    <div class="my-container">
        <div class="my-header user-info" v-if="user">
            <div class="base-info">
                <div class="left">
                    <van-image :src="userInfo.photo" class="avatar" round fit="cover" />
                    <span class="name">{{  userInfo.name  }}</span>
                </div>
                <div class="right">
                    <van-button type="default" size="mini" round to="/userInfo">编辑资料</van-button>
                </div>
            </div>
            <div class="data-stats">
                <div class="data-item">
                    <span class="count">{{  userInfo.art_count  }}</span>
                    <span class="text">头条</span>
                </div>
                <div class="data-item">
                    <span class="count">{{  userInfo.follow_count  }}</span>
                    <span class="text">关注</span>
                </div>
                <div class="data-item">
                    <span class="count">{{  userInfo.fans_count  }}</span>
                    <span class="text">粉丝</span>
                </div>
                <div class="data-item">
                    <span class="count">{{  userInfo.like_count  }}</span>
                    <span class="text">获赞</span>
                </div>
            </div>
        </div>
        <div class="my-header not-login" v-else>
            <div class="login-btn" @click="$router.push('./login')">
                <img src="~@/assets/mobile.png" alt="" class="mobile-img">
                <span class="login-text">登录 / 注册</span>
            </div>
        </div>
        <van-grid :column-num="2" class="grid-nav" clickable>
            <van-grid-item class="grid-item">
                <template #icon>
                    <i class="toutiao toutiao-shoucang"></i>
                </template>
                <template #text>
                    <span class="grid-item-text">收藏</span>
                </template>
            </van-grid-item>
            <van-grid-item class="grid-item">
                <template #icon>
                    <i class="toutiao toutiao-lishi"></i>
                </template>
                <template #text>
                    <span class="grid-item-text">历史</span>
                </template>
            </van-grid-item>
        </van-grid>
        <van-cell title="消息通知" is-link center />
        <van-cell title="小智同学" is-link center />
        <van-cell title="退出登录" center clickable class="logout-cell" v-if="user" @click="logout" size="large" />
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '../../api/user'

export default {
    name: 'MyIndex',
    data() {
        return {
            userInfo: {}
        }
    },
    created() {
        if (this.user) {
            this.userInfo = this.loadUserInfo()
        }
    },
    computed: {
        ...mapState(['user'])
    },
    methods: {
        logout() {
            this.$dialog.confirm({
                message: '确认退出？',
            }).then(() => {
                this.$store.commit('setUser', null)
            }).catch(() => {
                // on cancel
            })
        },
        async loadUserInfo() {
            try {
                const { data } = await getUserInfo()
                console.log(data);
                this.userInfo = data.data
            } catch (err) {
                this.$toast('获取数据失败，请稍后重试')
            }
        }
    }
}
</script>

<style scoped lang="less">
.my-container {
    .my-header {
        height: 360px;
        background-image: url('~@/assets/banner.png');
        background-size: cover;
    }

    .not-login {
        display: flex;
        justify-content: center;
        align-items: center;

        .login-btn {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;

            .mobile-img {
                width: 132px;
                height: 132px;
                margin-bottom: 15px;
            }

            .login-text {
                font-size: 28px;
                color: #fff;
            }
        }

    }

    .user-info {
        .base-info {
            height: 230px;
            box-sizing: border-box;
            padding: 76px 32px 23px;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .left {
                display: flex;
                align-items: center;

                .avatar {
                    width: 132px;
                    height: 132px;
                    border: 1px solid #fff;
                }

                .name {
                    font-size: 30px;
                    color: #fff;
                    margin-left: 20px;
                }
            }
        }

        .data-stats {
            height: 130px;
            display: flex;

            .data-item {
                flex: 1;
                height: 100%;
                display: flex;
                justify-content: center;
                align-items: center;
                flex-direction: column;
                color: #fff;

                .count {
                    font-size: 36px;
                }

                .text {
                    font-size: 23px;
                }
            }
        }
    }

    .grid-nav {
        height: 200px;

        i.toutiao {
            font-size: 45px;
        }

        i.toutiao-shoucang {
            color: #eb5253;
        }

        i.toutiao-lishi {
            color: #ff9d1d;
        }

        span.grid-item-text {
            font-size: 28px;
        }
    }

    .logout-cell {
        text-align: center;
        color: rgb(198, 57, 57);
        margin-top: 5px;
    }
}
</style>